package com.crm.realm;

import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;

/**
 * shiro配置类
 */
//@Configuration
public class ShiroConfig {

    /**
     * 过滤器工厂
     *
     * @param defaultWebSecurityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        //设置securityManager

        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //自定义过滤器，JwtFilter
        LinkedHashMap<String, Filter> filterMap = new LinkedHashMap<String, Filter>();
        filterMap.put("jwt", new OAuth2Filter());
        shiroFilterFactoryBean.setFilters(filterMap);
//        shiroFilterFactoryBean.setFilters();
        //设置拦截器，使用LinkedHashMap保证Filter的有序性
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        //公共资源或登录接口，无需进入过滤器直接执行即可
        filterChainDefinitionMap.put("/login/login", "anon");
        filterChainDefinitionMap.put("/guest/**", "anon");
      //放行swagger
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");

        filterChainDefinitionMap.put("/**", "jwt");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    /**
     * 配置shiro安全管理器 交给spring的ioc容器管理
     *
     * @param myRealm
     * @return
     */

    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("myRealm") MyRealm myRealm, @Qualifier("modularRealmAuthenticator") ModularRealmAuthenticator modularRealmAuthenticator) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setAuthenticator(modularRealmAuthenticator);
        manager.setRealm(myRealm);
        return manager;

    }

    /**
     * 我自己定义的myRealm
     *
     * @return
     */
    @Bean
    public MyRealm myRealm(@Qualifier("hashedCredentialsMatcher") CredentialsMatcher hashedCredentialsMatcher) {

        MyRealm myRealm = new MyRealm();
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return myRealm;
    }

    @Bean
    public CredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher md5 = new HashedCredentialsMatcher();
        md5.setHashIterations(3);
        md5.setHashAlgorithmName("md5");
        md5.setStoredCredentialsHexEncoded(true);
        return md5;
    }

    @Bean
    public ModularRealmAuthenticator modularRealmAuthenticator(@Qualifier("atLeastOneSuccessfulStrategy") AtLeastOneSuccessfulStrategy atLeastOneSuccessfulStrategy) {
        ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();
        modularRealmAuthenticator.setAuthenticationStrategy(atLeastOneSuccessfulStrategy);
        return modularRealmAuthenticator;
    }

    @Bean
    public AtLeastOneSuccessfulStrategy atLeastOneSuccessfulStrategy() {
        return new AtLeastOneSuccessfulStrategy();
    }



}
